package com.taikang.auth.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.taikang.auth.dao.IRightDao;
import com.taikang.auth.dao.IRoleDao;
import com.taikang.auth.entity.Right;
import com.taikang.auth.entity.User;
import com.taikang.auth.service.IRightService;
import com.taikang.base.constant.SystemConstant;

@Repository("rightServiceImpl")
@Transactional(value = "transactionManager")
public class RightServiceImpl implements IRightService{
	
	@Resource(name="rightDaoImpl")
	private IRightDao rightDao;
	@Resource(name="roleDaoImpl")
	private IRoleDao iroledao;
	
	public List<Right> listRightAll() {
		return rightDao.listRightAll();
	}

	public List<Right> listRightByUserId(String userId) {
		return rightDao.listRightByUserId(userId);
	}
	
	@Override
	public List<Right> listRightByRoleId(String roleId) {
		return rightDao.listRightByRoleId(roleId);
	}
	
	@Override
	public List<Right> listRightByParentId(String pid, Integer level) {
		List<Right> rights = listRightByPidLoop(pid, level);
		return rights;
	}
	
	@SuppressWarnings("unchecked")
	public String buildUserRightJSONStr(User user){
		String userRightJsonStr = null;
		List<Right> rights = new ArrayList<Right>();
		/*
		 * admin 拥有最大权限数据
		 */
		if (user.getAccount().equals(SystemConstant.ADMIN_USER)) {
			rights = rightDao.listRightAll();
		} else {
			rights = rightDao.listRightByUserId(user.getId());
		}

		JSONArray root = new JSONArray();
		// 构建权限
		for (int i = 0; i < rights.size(); i++) {
			Right permission = rights.get(i);
			if (permission.getLocation() == 1) { // 1级
				JSONObject first = new JSONObject();
				first.put("id", permission.getId());
				first.put("text", permission.getName());
				first.put("url", permission.getUrl());
				first.put("location", permission.getLocation());
				first.put("iconPath", permission.getIconPath());
				this.buildRights(first, permission, rights);

				root.add(first);
			}

		}

		userRightJsonStr = root.toJSONString();
		
		return userRightJsonStr;
	}
	
	public List<Right> buildUserRights(User user){
		List<Right> rights = new ArrayList<Right>();
		/*
		 * admin 拥有最大权限数据
		 */
		if (user.getAccount().equals(SystemConstant.ADMIN_USER)) {
			rights = rightDao.listRightAll();
		} else {
			rights = rightDao.listRightByUserId(user.getId());
		}
		return rights;
	}
	
	/**
	 * 构建权限
	 * 
	 * @param first
	 * @param permission
	 */
	@SuppressWarnings("unchecked")
	private void buildRights(JSONObject po, Right parent,List<Right> permissions) {
		JSONArray children = new JSONArray();
		po.put("children", children);
		for (int i = 0; i < permissions.size(); i++) {
			Right permission = permissions.get(i);
			if (null != permission.getParentId()
					&& permission.getParentId().equals(parent.getId())) {
				JSONObject node = new JSONObject();
				node.put("id", permission.getId());
				node.put("pcode", permission.getPcode());
				node.put("text", permission.getName());
				node.put("url", permission.getUrl());
				node.put("location", permission.getLocation());
				node.put("iconPath", permission.getIconPath());
				this.buildRights(node, permission, permissions);
				children.add(node);

			}
		}
	}
	
	/**
	 * 递归得到数据权限树
	 * @param pid
	 * @param level
	 * @return
	 */
	private List<Right> listRightByPidLoop(String pid, Integer level){
		
		// 符合条件的权限结果集
		List<Right> rights = null;
		
		// 如果级数 > 0，说明至少1级才能查出结果
		if(level > 0){
			// 查询PID下的权限
			rights = rightDao.listRightByParentId(pid);
			// 查过一级，级数-1
			level--;
			// 递归查询下级权限
			for(Right right : rights){
				// 逐一调当前的递归方法进行逐级下查
				List<Right> childRights = listRightByPidLoop(right.getId(), level);
				// 将子权限查询后存入权限实体中
				right.setChildRights(childRights);
			}
		}
		return rights;
	}
	
	@Override
	public Right getRightById(String id){
		return rightDao.getRightById(id);
	}

	@Override
	public void add(Right right) {
		rightDao.add(right);
	}
	
	@Override
	public void update(Right right) {
		rightDao.update(right);
	}
	
	@Override
	public void delByIds(String[] ids){
		rightDao.delByIds(ids);
	}
}
